﻿// Xshell TaskSet Condition - Current Windows User
// 
// <copyright file="WindowsUserCondition.cs" company="TaskyMedia LLC">
//   Copyright © 2010 TaskyMedia LLC
// </copyright>

namespace Xshell.Conditions
{
    using System;
    using System.Security.Principal;

    /// <summary>
    /// <see cref="XsCondition"/> implementation for checking the current logged in Windows username.
    /// </summary>
    [XsCondition("WindowsUser")]
    internal class WindowsUserCondition : XsCondition
    {
        /// <summary>
        /// Gets a value indicating whether the condition has been met.
        /// </summary>
        /// <value>
        /// <c>true</c> if this condition has been met; otherwise, <c>false</c>.
        /// </value>
        public override bool IsValid
        {
            get
            {
                WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                string currentUser = currentIdentity.Name;

                // Remove the domain from the check if necessary
                if (currentUser.Contains(@"\"))
                {
                    if (!Value.Contains(@"\"))
                    {
                        currentUser = currentUser.Substring(currentUser.IndexOf(@"\") + 1);
                    }
                }

                return currentUser == Value;
            }
        }
    }
}
